var PaginatedCollection = Backbone.Collection.extend({
	initialize: function() {
		_.bindAll(this, 'parse', 'url', 'pageInfo', 'nextPage', 'previousPage');
		typeof(options) != 'undefined' || (options = {});
		this.page = 1;
		typeof(this.perPage) != 'undefined' || (this.perPage = 10);
	},
	fetch: function(options) {
		typeof(options) != 'undefined' || (options = {});
		this.trigger("fetching");
		var self = this;
		var success = options.success;
		options.success = function(resp) {
			self.trigger("fetched");
			if (success) {
				success(self, resp);
			}
		};
		return Backbone.Collection.prototype.fetch.call(this, options);
	},
	parse: function(resp) {
		this.page = resp.page;
		this.perPage = resp.perPage;
		this.total = resp.total;
		return resp.models;
	},
	url: function() {
		return this.baseUrl + '?' + $.param({
			page: this.page,
			perPage: this.perPage
		});
	},
	pageInfo: function() {
		var info = {
			total: this.total,
			page: this.page,
			perPage: this.perPage,
			pages: Math.ceil(this.total / this.perPage),
			prev: false,
			next: false
		};

		var max = Math.min(this.total, this.page * this.perPage);

		if (this.total == this.pages * this.perPage) {
			max = this.total;
		}

		info.range = [(this.page - 1) * this.perPage + 1, max];

		if (this.page > 1) {
			info.prev = this.page - 1;
		}

		if (this.page < info.pages) {
			info.next = this.page + 1;
		}

		return info;
	},
	nextPage: function() {
		if (!this.pageInfo().next) {
			return false;
		}
		this.page = this.page + 1;
		return this.fetch();
	},
	previousPage: function() {
		if (!this.pageInfo().prev) {
			return false;
		}
		this.page = this.page - 1;
		return this.fetch();
	}

});

$('#nav_fav').tap(function(){
			App.popup({
				popupId: 'fav_result',
				autoClose: 4000,
				html:  '<div class="simple_msg"> <img src="./img/right.png" alt=""> 收藏成功 </div>',
			})
		})

		$('#nav_share').tap(function(){
			$('#select_share_list').show();

			$('body').not('#select_share_list').one('tap', function(){
				$('#select_share_list').hide();
			})
		});




var Storage = {
	get: function(k) {
		var cur = localStorage['App'],
			d = {};

		if ( typeof cur !== 'undefined' ) {
			d = win.JSON.parse(cur);
		}

		if ( k )
			return d[k];
		else
			return d;
	},

	set: function(k, v) {
		var d = Storage.get();

		d[k] = v;
		Storage.save(d);
	},

	save: function(d) {
		localStorage['App'] = win.JSON.stringify(d);
	},

	remove: function(k) {
		if ( Storage.isStored(k) ) {
			var d = Storage.get();

			delete d[k];
			Storage.save(d);
		}
	},

	isStored: function(k) {
		return ( k in Storage.get() );
	}
};


function paramsToString( obj ) {
	var ary = [];

	for ( var key in obj ) {
		ary.push( key + '=' + encodeURIComponent( obj[ key ] ) );
	}

	return ary.join( '&' );
}




// my bbs
if(channel == "receive"){
	if(!App.view.myBbsR){
		App.collection.myBbsR = new MyBbs('', {channel: channel});
		App.view.myBbsR = new MyBbsView({
			collection: App.collection.myBbsR,
			id: 'myBbs_' + channel
		});
	} else {
		App.view.myBbsR.show();
	}
} else if (channel == 'send'){
	if(!App.view.myBbsS){
		App.collection.myBbsS = new MyBbs('', {channel: channel});
		App.view.myBbsS = new MyBbsView({
			collection: App.collection.myBbsS,
			id: 'myBbs_' + channel
		});
	} else {
		App.view.myBbsS.show();
	}
} else if (channel == 'collect'){
	if(!App.view.myBbsC){
		App.collection.myBbsC = new MyBbs('', {channel: channel});
		App.view.myBbsC = new MyBbsView({
			collection: App.collection.myBbsC,
			id: 'myBbs_' + channel
		});
	} else {
		App.view.myBbsC.show();
	}
} else {
	// do nothing
}